﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using LumaQQ.NET;
using LumaQQ.NET.Utils;
using LumaQQ.NET.Events;
using LumaQQ.NET.Entities;
using LumaQQ.NET.Packets;
using LumaQQ.NET.Packets.In;
using LumaQQ.NET.Packets.Out;

namespace QMIM
{
    public class QQMain
    {
        public QQUser qquser { get; private set; }
        public QQClient qqclient { get; private set; }
        
        public QQMain()
        {
            qquser = null;
            qqclient = null;
           
        }

        #region events
        public event EventHandler LoginFailed;
        
        private void OnLoginFailed(EventArgs e)
        {
            if (LoginFailed != null)
            {
                LoginFailed(this, e);
            }
        }

        public event EventHandler LoginSuccessed;
        private void OnLoginSuccess(EventArgs e)
        {
            if (LoginSuccessed != null)
            {
                LoginSuccessed(this, e);
            }

        }

        #endregion

        public void DoLogin(int qq, string passwd)
        {
            qquser = new QQUser(qq, passwd);
            qqclient = new QQClient(qquser);
            QQForm.qqClient = qqclient;
            Init();
            qqclient.Login();
           
        }

        private void Init()
        {
            qquser.IsUdp = false;
            qqclient.LoginPort = 80;
            qqclient.LoginServerHost = "219.133.60.149";                        

            #region login events
            qqclient.LoginManager.LoginUnknownError += new EventHandler<QQEventArgs<LoginReplyPacket, LoginPacket>>(LoginManager_LoginUnknownError);
            qqclient.LoginManager.LoginSuccessed += new EventHandler<LumaQQ.NET.Events.QQEventArgs<LumaQQ.NET.Packets.In.LoginReplyPacket, LoginPacket>>(LoginManager_LoginSuccessed);
            qqclient.LoginManager.LoginFailed += new EventHandler<LumaQQ.NET.Events.QQEventArgs<LumaQQ.NET.Packets.In.LoginReplyPacket, LoginPacket>>(LoginManager_LoginFailed);
            qqclient.LoginManager.LoginRedirect += new EventHandler<LumaQQ.NET.Events.QQEventArgs<LumaQQ.NET.Packets.In.LoginReplyPacket, LoginPacket>>(LoginManager_LoginRedirect);
            #endregion
        }

        private void LoginManager_LoginUnknownError(object sender, QQEventArgs<LoginReplyPacket, LoginPacket> e)
        {
            OnLoginFailed(e);
        }

        private void LoginManager_LoginSuccessed(object sender, LumaQQ.NET.Events.QQEventArgs<LumaQQ.NET.Packets.In.LoginReplyPacket, LoginPacket> e)
        {
            OnLoginSuccess(e);
            

        }

        private void LoginManager_LoginFailed(object sender, LumaQQ.NET.Events.QQEventArgs<LumaQQ.NET.Packets.In.LoginReplyPacket, LoginPacket> e)
        {
            OnLoginFailed(e);
        }

        private void LoginManager_LoginRedirect(object sender, LumaQQ.NET.Events.QQEventArgs<LumaQQ.NET.Packets.In.LoginReplyPacket, LoginPacket> e)
        {
            string s = "重定向登录，服务器IP：" + e.InPacket.RedirectIPString;
         //   qqclient.LoginServerHost = e.InPacket.RedirectIPString;
         //   qqclient.Login();
        }

    }
}
